package com.card323.crpg.battle;

import java.awt.Color;

import com.card323.crpg.Hero;
import com.card323.crpg.HeroTeam;
import com.card323.crpg.exception.CRPGWrongIndexException;

public class BattleEngine {

	public HeroTeam _team1;
	public HeroTeam _team2;

	public volatile boolean _is_end;

	public BattleEngine(HeroTeam allies_team, HeroTeam enemy_team) {
		_is_end = false;
		_team1 = allies_team;
		_team2 = enemy_team;
	}

	public void broadcast_message(String msg) {
		for (Hero h : _team1._heros) {
			h.send_message(msg);
		}
		for (Hero h : _team2._heros) {
			h.send_message(msg);
		}
	}

	public void start() {
		print_battle_info();

		for (Hero h : _team1._heros) {
			h.get_bi().battle_start(this);
			//h.set_buffs();
			if (h._is_ai) {
				HeroBattleAI ai = new HeroBattleAI(h, this);
				Thread t = new Thread(ai);
				t.start();
			}
		}
		for (Hero h : _team2._heros) {
			h.get_bi().battle_start(this);
			//h.set_buffs();
			if (h._is_ai) {
				HeroBattleAI ai = new HeroBattleAI(h, this);
				Thread t = new Thread(ai);
				t.start();
			}
		}
		Thread t = new Thread(new BattleTurn(this));
		t.start();
	}

	private void print_battle_info() {
		for (Hero h : _team1._heros) {

			h.send_message("战斗开始 .", Color.RED);
			for (Hero enemy_hero : get_enemy_team(h)._heros) {
				h.send_message(enemy_hero.get_name() + "出现了");
			}
			h.send_message("请输入战斗指令",Color.BLUE);
		}

		for (Hero h : _team2._heros) {

			h.send_message("战斗开始 .", Color.RED);
			for (Hero enemy_hero : get_enemy_team(h)._heros) {
				h.send_message(enemy_hero.get_name() + "出现了");
			}
			h.send_message("请输入战斗指令",Color.BLUE);

		}

	}

	public void attack(Hero me, Hero enemy) {

		me.get_bi().send_message_to_all(me.get_name() + " 攻击 " + enemy.get_name());
		me.get_bi().get_ap().minus_action_point(me._attack_speed);
		int dmg = me.get_bi().get_attack_dmg();
		me.get_bi().send_message_to_allies(enemy.get_name() + " 受到伤害 " + dmg, Color.CYAN);
		me.get_bi().send_message_to_enemy(enemy.get_name() + " 受到伤害 " + dmg + ".生命值 "
				+ enemy.get_current_life() + "->" + (enemy.get_current_life() - dmg),
				Color.RED);
		enemy.set_current_life(enemy.get_current_life() - dmg);
		me.get_bi().raise_event(new AfterAttackEvent(me,enemy,dmg));
		enemy.get_bi().raise_event(new AfterBeAttackEvent(me,enemy,dmg));
		if (enemy.is_died()) {
			me.get_bi().send_message_to_allies(enemy.get_name() + " 挂了 ", Color.BLACK);
			me.get_bi().send_message_to_enemy(enemy.get_name() + " 挂了 ", Color.RED);
		}
		if (get_enemy_team(me).is_all_dead()) {
			_is_end = true;
			me.get_bi().send_message_to_all("战斗结束");
			battle_over();
		}

	}

	private void battle_over() {
		for (Hero h : _team1._heros) {
			h.get_bi().battle_end();
		}
		for (Hero h : _team2._heros) {
			h.get_bi().battle_end();
		}

	}



	public HeroTeam get_enemy_team(Hero _hero) {
		if (_team1._heros.contains(_hero))
			return _team2;
		else
			return _team1;
	}

	public HeroTeam get_allies_team(Hero _hero) {
		if (_team1._heros.contains(_hero))
			return _team1;
		else
			return _team2;
	}

	public String get_targets_list(Hero hero) {
		StringBuffer sb = new StringBuffer();
		HeroTeam allies = get_allies_team(hero);
		HeroTeam enemy = get_enemy_team(hero);
		for (int i = 0; i < 5; i++) {
			String name = "";
			if (i < allies._heros.size())
				name = allies._heros.get(i).get_name();
			int index = (i + 1) % 10;
			sb.append(index + ":" + name + "\n");

		}
		for (int i = 0; i < 5; i++) {
			String name = "";
			if (i < enemy._heros.size())
				name = enemy._heros.get(i).get_name();
			int index = (i + 6) % 10;
			sb.append(index + ":" + name + "\n");
		}

		return sb.toString();
	}
	
	public Hero get_target_hero(Hero hero,int index) throws CRPGWrongIndexException
	{
		if(index==0)
			index=10;
		if(index<=5)
		{
			index-=1;
			HeroTeam allies = get_allies_team(hero);
			
			if(index<allies._heros.size())
			{
				return allies._heros.get(index);
			}
			else
			{
				throw new CRPGWrongIndexException("wrong index");
			}
		}
		else
		{
			index-=6;
			HeroTeam enemy = get_enemy_team(hero);
			if(index<enemy._heros.size())
			{
				return enemy._heros.get(index);
			}
			else
				throw new CRPGWrongIndexException("wrong index");
		}
	}

	public void attack(Hero hero) {
		attack(hero,get_enemy_team(hero).get_hatred_hero());
		
	}

}
